/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.inv.input.service.spring;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.erp.inv.input.dao.model.InvInputLineVO;
import com.erp.masterdata.common.param.MasterDataParam;
import com.erp.order.dn.dao.model.DeliveryNoteLine;
import com.erp.order.dn.service.DeliveryNoteLineService;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.inv.input.dao.InvInputLineDao;
import com.erp.inv.input.dao.model.InvInputHead;
import com.erp.inv.input.dao.model.InvInputLine;
import com.erp.inv.input.dao.model.InvInputLineCO;
import com.erp.inv.input.service.InvInputHeadService;
import com.erp.inv.input.service.InvInputLineService;
import com.erp.inv.stock.dao.model.InvStock;
import com.erp.inv.stock.service.InvStockService;

@Service
@Transactional(rollbackFor=Exception.class)
public class InvInputLineServiceImpl implements InvInputLineService {

    //注入Dao
    @Autowired
    private InvInputLineDao invInputLineDao;
    @Autowired
    private InvStockService invStockService;
    @Autowired
    private InvInputHeadService invInputHeadService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private DeliveryNoteLineService deliveryNoteLineService;
    
    @Override
    public void insertDataObject(InvInputLine obj) {
        this.invInputLineDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(InvInputLine obj) {
        this.invInputLineDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(InvInputLine obj) {
        this.invInputLineDao.insertOrUpdateDataObject(obj);
        //插入库存（保留）
        if(obj.getHeadInputType().equals("PO_RETURN")||obj.getHeadInputType().equals("PROD_RETURN")) {
            //删除库存
            this.invStockService.deleteInvStockByBillLineCode("INPUT%", obj.getInputHeadCode(), obj.getInputLineCode());
            
            //获取头
            InvInputHead head = this.invInputHeadService.getDataObject(obj.getInputHeadCode());
            
            InvStock stock = new InvStock();
            stock.setBillHeadCode(obj.getInputHeadCode());
            stock.setBillLineCode(obj.getInputLineCode());
            stock.setBillType("INPUT");
            stock.setRetainFlag("Y");
            stock.setMaterialCode(obj.getMaterialCode());
            if(obj.getHeadInputType().equals("PO_RETURN")) {
                stock.setMemo("入库退货单自动创建");
            }else if(obj.getHeadInputType().equals("PROD_RETURN")) {
                stock.setMemo("生产退料单自动创建");
            }
            stock.setStockCode(SnowFlake.getConcurrentId().toString());
            stock.setStockNumber(obj.getInputQuantity().multiply(new BigDecimal(-1)));
            stock.setWarehouseCode(head.getWarehouseCode());
            //获取当前用户职员信息
            HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
            stock.setStaffCode(staffInfo.getStaffCode());
            stock.setDepartmentCode(staffInfo.getDepartmentCode());
            
            this.invStockService.insertDataObject(stock);
        }
    }

    @Override
    public void deleteDataObject(InvInputLine obj) {
        //删除库存
        InvInputLine temp = this.invInputLineDao.getDataObject(obj.getInputLineId());
//        this.invStockService.deleteInvStockByBillLineCode("INPUT%", temp.getInputHeadCode(), temp.getInputLineCode());
        //删除入库单数据
        DeliveryNoteLine noteLine = deliveryNoteLineService.getDataObject(Integer.valueOf(temp.getInputSourceLineCode()));
        noteLine.setStatus(MasterDataParam.DELIVERY_STATUS_N);
        deliveryNoteLineService.insertOrUpdateDataObject(noteLine);
        temp.setIsDel(MasterDataParam.IS_DEL_YES);
        this.invInputLineDao.insertOrUpdateDataObject(temp);
    }

    @Override
    public List<InvInputLine> getDataObjects() {
        return this.invInputLineDao.getDataObjects();
    }

    @Override
    public InvInputLine getDataObject(int id) {
        return this.invInputLineDao.getDataObject(id);
    }

    @Override
    public InvInputLine getDataObject(String code) {
        return this.invInputLineDao.getDataObject(code);
    }

    @Override
    public List<InvInputLine> getDataObjects(InvInputLineCO paramObj) {
        return this.invInputLineDao.getDataObjects(paramObj);
    }

    @Override
    public List<InvInputLine> getDataObjects(Pages pages) {
        return this.invInputLineDao.getDataObjects(pages);
    }
    
    @Override
    public List<InvInputLine> getDataObjects(Pages pages, InvInputLineCO paramObj) {
        return this.invInputLineDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, InvInputLineCO paramObj) {
        return this.invInputLineDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<InvInputLine> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, InvInputLineCO paramObj) {
        return this.invInputLineDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public List<InvInputLine> getInvInputLineListByInputHeadCode(Pages pages, InvInputLineCO paramObj) {
        return this.invInputLineDao.getInvInputLineListByInputHeadCode(pages, paramObj);
    }
    
    @Override
    public void deleteInvInputLineByInputHeadCode(String inputHeadCode) {
        //this.invInputLineDao.deleteInvInputLineByInputHeadCode(inputHeadCode);
        List<InvInputLine> list = this.getInvInputLineListByInputHeadCode(inputHeadCode);
        for(InvInputLine invInputLine: list) {
            invInputLine.setIsDel(MasterDataParam.IS_DEL_YES);
            insertOrUpdateDataObject(invInputLine);
//            this.deleteDataObject(invInputLine);
        }
    }
    
    @Override
    public BigDecimal getInputQuantityByPoLineCode(String poLineCode) {
        return this.invInputLineDao.getInputQuantityByPoLineCode(poLineCode);
    }
    
    @Override
    public List<InvInputLine> getInvInputLineListByInputHeadCode(String inputHeadCode) {
        return this.invInputLineDao.getInvInputLineListByInputHeadCode(inputHeadCode);
    }
    
    @Override
    public BigDecimal getInputedQuantityByPoLine(String poLineCode, Integer inputLineId) {
        return this.invInputLineDao.getInputedQuantityByPoLine(poLineCode, inputLineId);
    }

    @Override
    public List<InvInputLineVO> getInputLineAndMaterial(Pages pages, InvInputLineVO invInputLineVO, String supply_material_id){
        return this.invInputLineDao.getInputLineAndMaterial(pages,invInputLineVO,supply_material_id);
    }
}